<template>
  <Form ref="formValidate"
        :model="formValidate"
        :rules="ruleValidate"
        :label-width="110"
        class="box">
    <FormItem label="营业日结算时间:"
              class="aa">
      <Row>
        <Col>
        <FormItem prop="time">
          <TimePicker type="time"
                      placeholder="请选择时间"
                      :value="formValidate.time"
                      format="HH:mm"
                      size="large"
                      :editable="false"
                      style="width:280px"
                      @on-change='onChange'></TimePicker>
        </FormItem>
        </Col>
      </Row>
    </FormItem>
    <FormItem>
      <Button type="primary"
              @click="handleSubmit('formValidate')"
              style="width:120px">确认</Button>
      <Button style="width:106px;margin-left:20px"
              @click='cancel'>取消</Button>
    </FormItem>
  </Form>
</template>
<script>
import { routeEqual } from '@/libs/util'
import { getDicQuery, setDicSave } from '@/api/setting'
export default {
  data () {
    return {
      formValidate: {
        date: '',
        time: '00:00'
      },
      ruleValidate: {
        time: [
          { required: true, type: 'string', message: '值不能为空', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
  },
  created () {
  },
  mounted () {
    this.submitForm()
    if (!this.hasPermission('moms:globalsetting:businesshours')) {
      this.$router.replace({ name: 'error_401' })
    }
  },
  methods: {
    onChange (val) {
      this.formValidate.time = val
    },
    handleSubmit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          setDicSave({
            'msType': '0',
            'businessDayTime': this.formValidate.time,
            'merchantId': this.$store.state.user.merchantId
          }).then((res) => {
            if (res.data.status === 200) {
              this.$Message.success('设置成功')
              this.closeModal()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        }
      })
    },
    // 关闭弹窗
    closeModal () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'global_setting'
        })
      })
    },
    cancel () {
      this.closeModal()
    },
    submitForm () {
      getDicQuery({
        'msType': '0',
        'dictKeys': 'businessDayTime',
        'merchantId': this.$store.state.user.merchantId
      }).then((res) => {
        if (res.data.status === 200) {
          if (res.data.data[0].businessDayTime === 0 || res.data.data[0].businessDayTime === '0') {
            this.formValidate.time = '00:00'
          } else {
            this.formValidate.time = res.data.data[0].businessDayTime
          }
        } else {
          this.$Message.error(`${res.data.error}`)
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.box {
  width: 600px;
  margin: 0 auto;
  padding-top: 100px;
}
</style>
